package middleware

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"shop-api/pkg/jwt"
)

func VerifyToken(c *gin.Context) {
	token := c.GetHeader("Authorization")
	if token == "" {
		c.JSON(http.StatusUnauthorized, gin.H{"code": -1, "data": nil, "msg": "token is required"})
		c.Abort()
		return
	}

	userID, err := jwt.VerifyToken(token)
	if err != nil {
		c.JSON(http.StatusUnauthorized, gin.H{"code": -1, "data": nil, "msg": "token is invalid"})
		c.Abort()
		return
	}

	c.Set("user_id", userID)
	c.Next()
}
